home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / msoftapp.zip / DIBDOC.CPP < prev    next >
C/C++ Source or Header  |  1993-06-01  |  5KB  |  225 lines

  1. // dibdoc.cpp : implementation of the CDibDoc class
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and Microsoft
  9. // QuickHelp and/or WinHelp documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13. #include "stdafx.h"
  14. #include "diblook.h"
  15. #include <limits.h>
  16.  
  17. #include "dibdoc.h"
  18.  
  19. #ifdef _DEBUG
  20. #undef THIS_FILE
  21. static char BASED_CODE THIS_FILE[] = __FILE__;
  22. #endif
  23.  
  24. /////////////////////////////////////////////////////////////////////////////
  25. // CDibDoc
  26.  
  27. IMPLEMENT_DYNCREATE(CDibDoc, CDocument)
  28.  
  29. BEGIN_MESSAGE_MAP(CDibDoc, CDocument)
  30.     //{{AFX_MSG_MAP(CDibDoc)
  31.     //}}AFX_MSG_MAP
  32. END_MESSAGE_MAP()
  33.  
  34. /////////////////////////////////////////////////////////////////////////////
  35. // CDibDoc construction/destruction
  36.  
  37. CDibDoc::CDibDoc()
  38. {
  39.     m_hDIB = NULL;
  40.     m_palDIB = NULL;
  41.     m_sizeDoc = CSize(1,1);     // dummy value to make CScrollView happy
  42. }
  43.  
  44. CDibDoc::~CDibDoc()
  45. {
  46.     if (m_hDIB != NULL)
  47.     {
  48.         ::GlobalFree((HGLOBAL) m_hDIB);
  49.     }
  50.     if (m_palDIB != NULL)
  51.     {
  52.         delete m_palDIB;
  53.     }
  54. }
  55.  
  56. BOOL CDibDoc::OnNewDocument()
  57. {
  58.     if (!CDocument::OnNewDocument())
  59.         return FALSE;
  60.     return TRUE;
  61. }
  62.  
  63. void CDibDoc::InitDIBData()
  64. {
  65.     if (m_palDIB != NULL)
  66.     {
  67.         delete m_palDIB;
  68.         m_palDIB = NULL;
  69.     }
  70.     if (m_hDIB == NULL)
  71.     {
  72.         return;
  73.     }
  74.     // Set up document size
  75.     LPSTR lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) m_hDIB);
  76.     if (::DIBWidth(lpDIB) > INT_MAX ||::DIBHeight(lpDIB) > INT_MAX)
  77.     {
  78.         ::GlobalUnlock((HGLOBAL) m_hDIB);
  79.         ::GlobalFree((HGLOBAL) m_hDIB);
  80.         m_hDIB = NULL;
  81.         MessageBox(NULL, "DIB is too large", NULL,
  82.                          MB_ICONINFORMATION | MB_OK);
  83.         return;
  84.     }
  85.     m_sizeDoc = CSize((int) ::DIBWidth(lpDIB), (int) ::DIBHeight(lpDIB));
  86.     ::GlobalUnlock((HGLOBAL) m_hDIB);
  87.     // Create copy of palette
  88.     m_palDIB = new CPalette;
  89.     if (m_palDIB == NULL)
  90.     {
  91.         // we must be really low on memory
  92.         ::GlobalFree((HGLOBAL) m_hDIB);
  93.         m_hDIB = NULL;
  94.         return;
  95.     }
  96.     if (::CreateDIBPalette(m_hDIB, m_palDIB) == NULL)
  97.     {
  98.         // DIB may not have a palette
  99.         delete m_palDIB;
  100.         m_palDIB = NULL;
  101.         return;
  102.     }
  103. }
  104.  
  105.  
  106. BOOL CDibDoc::OnOpenDocument(const char* pszPathName)
  107. {
  108.     CFile file;
  109.     CFileException fe;
  110.     if (!file.Open(pszPathName, CFile::modeRead | CFile::shareDenyWrite, &fe))
  111.     {
  112.         ReportSaveLoadException(pszPathName, &fe,
  113.             FALSE, AFX_IDP_FAILED_TO_OPEN_DOC);
  114.         return FALSE;
  115.     }
  116.  
  117.     DeleteContents();
  118.     BeginWaitCursor();
  119.  
  120.     // replace calls to Serialize with ReadDIBFile function
  121.     TRY
  122.     {
  123.         m_hDIB = ::ReadDIBFile(file);
  124.     }
  125.     CATCH (CFileException, eLoad)
  126.     {
  127.         file.Abort(); // will not throw an exception
  128.         EndWaitCursor();
  129.         ReportSaveLoadException(pszPathName, eLoad,
  130.             FALSE, AFX_IDP_FAILED_TO_OPEN_DOC);
  131.         m_hDIB = NULL;
  132.         return FALSE;
  133.     }
  134.     END_CATCH
  135.  
  136.     InitDIBData();
  137.     EndWaitCursor();
  138.  
  139.     if (m_hDIB == NULL)
  140.     {
  141.         // may not be DIB format
  142.         MessageBox(NULL, "Couldn't load DIB", NULL,
  143.                          MB_ICONINFORMATION | MB_OK);
  144.         return FALSE;
  145.     }
  146.     SetPathName(pszPathName);
  147.     SetModifiedFlag(FALSE);     // start off with unmodified
  148.     return TRUE;
  149. }
  150.  
  151.  
  152. BOOL CDibDoc::OnSaveDocument(const char* pszPathName)
  153. {
  154.     CFile file;
  155.     CFileException fe;
  156.  
  157.     if (!file.Open(pszPathName, CFile::modeCreate |
  158.       CFile::modeReadWrite | CFile::shareExclusive, &fe))
  159.     {
  160.         ReportSaveLoadException(pszPathName, &fe,
  161.             TRUE, AFX_IDP_INVALID_FILENAME);
  162.         return FALSE;
  163.     }
  164.  
  165.     // replace calls to Serialize with SaveDIB function
  166.     BOOL bSuccess = FALSE;
  167.     TRY
  168.     {
  169.         BeginWaitCursor();
  170.         bSuccess = ::SaveDIB(m_hDIB, file);
  171.         file.Close();
  172.     }
  173.     CATCH (CException, eSave)
  174.     {
  175.         file.Abort(); // will not throw an exception
  176.         EndWaitCursor();
  177.         ReportSaveLoadException(pszPathName, eSave,
  178.             TRUE, AFX_IDP_FAILED_TO_SAVE_DOC);
  179.         return FALSE;
  180.     }
  181.     END_CATCH
  182.  
  183.     EndWaitCursor();
  184.     SetModifiedFlag(FALSE);     // back to unmodified
  185.  
  186.     if (!bSuccess)
  187.     {
  188.         // may be other-style DIB (load supported but not save)
  189.         //  or other problem in SaveDIB
  190.         MessageBox(NULL, "Couldn't save DIB", NULL,
  191.                          MB_ICONINFORMATION | MB_OK);
  192.     }
  193.  
  194.     return bSuccess;
  195. }
  196.  
  197. void CDibDoc::ReplaceHDIB(HDIB hDIB)
  198. {
  199.     if (m_hDIB != NULL)
  200.     {
  201.         ::GlobalFree((HGLOBAL) m_hDIB);
  202.     }
  203.     m_hDIB = hDIB;
  204. }
  205.  
  206. /////////////////////////////////////////////////////////////////////////////
  207. // CDibDoc diagnostics
  208.  
  209. #ifdef _DEBUG
  210. void CDibDoc::AssertValid() const
  211. {
  212.     CDocument::AssertValid();
  213. }
  214.  
  215. void CDibDoc::Dump(CDumpContext& dc) const
  216. {
  217.     CDocument::Dump(dc);
  218. }
  219.  
  220. #endif //_DEBUG
  221.  
  222. /////////////////////////////////////////////////////////////////////////////
  223. // CDibDoc commands
  224.  
  225.